package com.sadam.ui4.FragmentMessagePage;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.sadam.ui4.Data.Video;
import com.sadam.ui4.R;

import java.util.List;

/**
 * 点赞消息适配器
 */
public class LikeMessageAdapter extends RecyclerView.Adapter<LikeMessageAdapter.ViewHolder> {

    private List<LikeMessage> likeMessages;
    private OnMessageClickListener onMessageClickListener;

    public interface OnMessageClickListener {
        void onMessageClick(Video video);
    }

    public void setOnMessageClickListener(OnMessageClickListener listener) {
        this.onMessageClickListener = listener;
    }

    public LikeMessageAdapter(List<LikeMessage> likeMessages) {
        this.likeMessages = likeMessages;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_message_like, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LikeMessage message = likeMessages.get(position);

        holder.tvUsername.setText(message.getUsername());
        holder.tvContent.setText("点赞了你的视频");
        holder.tvTime.setText(message.getTimeAgo());
        holder.tvVideoTitle.setText(message.getVideoTitle());

        // 加载头像
        if (message.getAvatarUrl() != null && !message.getAvatarUrl().isEmpty()) {
            Glide.with(holder.itemView.getContext())
                    .load(message.getAvatarUrl())
                    .placeholder(R.drawable.user_avatar)
                    .error(R.drawable.user_avatar)
                    .into(holder.ivAvatar);
        } else {
            holder.ivAvatar.setImageResource(R.drawable.user_avatar);
        }

        // 🔽🔽🔽 使用 Glide 加载视频封面 🔽🔽🔽
        loadVideoCoverWithGlide(message.getVideo(), holder.ivVideoCover);

        // 添加整个item的点击事件
        holder.itemView.setOnClickListener(v -> {
            if (onMessageClickListener != null && message.getVideo() != null) {
                onMessageClickListener.onMessageClick(message.getVideo());
            }
        });
    }

    @Override
    public int getItemCount() {
        return likeMessages.size();
    }

    /**
     * 使用 Glide 加载视频封面
     */
    private void loadVideoCoverWithGlide(Video video, ImageView imageView) {
        if (video == null || video.getVideoFilePath() == null) {
            imageView.setImageResource(R.drawable.sample_cover);
            return;
        }

        // 使用 Glide 加载视频封面
        Glide.with(imageView.getContext())
                .load(video.getVideoFilePath()) // 直接使用视频文件路径
                .placeholder(R.drawable.sample_cover) // 加载中显示默认封面
                .error(R.drawable.sample_cover)       // 加载失败显示默认封面
                .thumbnail(0.1f) // 缩略图，提升加载速度
                .into(imageView);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAvatar;
        ImageView ivVideoCover;
        TextView tvUsername;
        TextView tvContent;
        TextView tvTime;
        TextView tvVideoTitle;

        ViewHolder(View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            ivVideoCover = itemView.findViewById(R.id.ivVideoCover);
            tvUsername = itemView.findViewById(R.id.tvUsername);
            tvContent = itemView.findViewById(R.id.tvContent);
            tvTime = itemView.findViewById(R.id.tvTime);
            tvVideoTitle = itemView.findViewById(R.id.tvVideoTitle);
        }
    }
}